package com.hh.demo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hh.demo.entity.Toy;
import com.hh.demo.service.IToyService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吉吉国王
 * @since 2022-12-05
 */
@Controller
public class ToyController {
    @Resource
    private IToyService toyService;
    Date date = new Date();
    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    //保存玩具数据
    @PostMapping("/toyData")
    @ResponseBody
    public void saveToyData(String number,String price,String count,String failure,String isscrap,String toyCount){
        Toy toy = new Toy();
        toy.setCount(toyCount);
        toy.setAttachCount(count);
        toy.setDate(format.format(date));
        toy.setPrice(price);
        toy.setId(number);
        toy.setFailure(failure);
        toy.setIsScrap(isscrap);
        toyService.save(toy);
    }
    //按照玩具编号查询数量
    @RequestMapping("/queryToyCount")
    @ResponseBody
    public List<Toy> queryToyCount(){
         return toyService.queryAll();
    }

    //查询全部信息*
    @RequestMapping("/queryAllToy")
    public String queryAllToy(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 5);//设置页面的位置和展示的数据条目数
        List<Toy> list = toyService.queryAll();
        PageInfo<Toy> pageInfo = new PageInfo<>(list);//PageInfo用来封装页面信息，返回给前台界面
        model.addAttribute("pageInfo", pageInfo);
        return "queryAllToy";
    }
}
